<template>
	<view class="main">
		<view class="title">
			安全验证
		</view>
		<view class="verify">
			<view class="meno">
				请输入{{userinfo.mobile}}收到的短信验证码
			</view>
			<view class="v-code">
				<input class="oldcode" type="number" maxlength="6" v-model="oldcode" @input="oldcodeInput" placeholder="请输入验证码">
				<view class="v-code-btn" @click="sendCode">
					{{oldcodetext}}
				</view>
			</view>
		</view>
		<view v-if="is_show" class="form">
			<view class="meno">
				新手机号验证
			</view>
			<view class="item">
				<view class="phone">
					<input type="number" maxlength="11" v-model="phone" placeholder="请输入要更换的手机号">
				</view>
				<view class="new-code">
					<input class="code" type="number" maxlength="6" v-model="code" placeholder="请输入验证码">
					<view class="new-code-btn" @click="sendNewCode">
						{{newcodetext}}
					</view>
				</view>
			</view>
		</view>
		<view v-if="!is_show" class="bottom-btn" :class="{'b-on' : is_safety_verify}" @click="verify">
			下一步
		</view>
		<view v-else class="bottom-btn b-on" @click="saveData">
			确定
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {},
				oldcodetext: '获取验证码',
				oldcode: '',
				phone: '',
				newcodetext: '获取验证码',
				code: '',
				btn_text: '下一步',
				isCodeTrueA: true,
				isCodeTrueB: true,
				is_show: false,
				is_safety_verify: false,
				Timer: null,
				TimerB: null
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo')
		},
		methods: {
			// 更换手机号
			async saveData() {
				var that = this
				if (this.phone == '') {
					uni.showToast({
						title: '请输入要更换的手机号',
						icon: 'none'
					})
					return false
				} else {
					if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(that.phone))) {
						uni.showToast({
							title: '请输入正确的手机号码',
							icon: "none"
						});
						return false;
					}
				}
				if (this.code == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return false
				}
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'User/verifyTarget',
					data: {
						mobile: this.phone,
						code: this.code
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (res.code == 1) {
					setTimeout(function() {
						that._Logout()
					}, 1500)
				}
			},
			// 安全验证 验证手机号
			async verify() {
				if (!this.is_show && this.is_safety_verify) {
					if (this.oldcode == '') {
						uni.showToast({
							title: '请输入验证码',
							icon: 'none'
						})
						return false
					}
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'User/verifyOwn',
						data: {
							mobile: this.userinfo.username,
							code: this.oldcode
						},
						header: {
							'token': this.userinfo.token
						}
					});
					uni.hideLoading()
					if (res.code == 1) {
						this.is_show = true
						this.is_safety_verify = false
						this.isCodeTrueA = false
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
			},
			// 安全验证 - 发送验证码
			async sendCode() {
				if (this.isCodeTrueA) {
					this.isCodeTrueA = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'V1/sendSms',
						data: {
							mobile: this.userinfo.username
						},
						header: {
							'token': this.userinfo.token
						}
					});
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						let holdTime = 60; //定义变量并赋值
						this.oldcodetext = "重新获取(60)"
						this.Timer = setInterval(() => {
							if (holdTime <= 0) {
								this.isCodeTrueA = true;
								this.oldcodetext = "获取验证码"
								clearInterval(this.Timer); //清除该函数
								return; //返回前面
							}
							this.oldcodetext = "重新获取(" + holdTime + ")"
							holdTime--;
						}, 1000)
					}
				}
			},
			// 发送新手机号验证码
			async sendNewCode() {
				var that = this
				if (this.phone == '') {
					uni.showToast({
						title: '请输入要更换的手机号',
						icon: 'none'
					})
					return false
				} else {
					if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(that.phone))) {
						uni.showToast({
							title: '请输入正确的手机号码',
							icon: "none"
						});
						return false;
					}
				}
				if (this.isCodeTrueB) {
					this.isCodeTrueB = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'V1/sendSms',
						data: {
							mobile: this.phone,
							type: 3
						},
						header: {
							'token': this.userinfo.token
						}
					});
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						let holdTimes = 60; //定义变量并赋值
						this.newcodetext = "重新获取(60)"
						this.TimerB = setInterval(() => {
							if (holdTimes <= 0) {
								this.isCodeTrueB = true;
								this.newcodetext = "获取验证码"
								clearInterval(this.TimerB); //清除该函数
								return; //返回前面
							}
							this.newcodetext = "重新获取(" + holdTimes + ")"
							holdTimes--;
						}, 1000)
					} else {
						this.isCodeTrueB = true;
					}
				}
			},
			oldcodeInput(e) {
				if (e.detail.value.length == 6) {
					this.is_safety_verify = true
				} else {
					this.is_safety_verify = false
				}
			},
			async _Logout() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'user/logout',
					data: {},
					header: {
						'token': this.userinfo.token
					},
				});
				if (res.code == 1) {
					uni.removeStorageSync('userinfo');
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 30rpx;
		.title {
			padding: 60rpx 0;
			font-size: 40rpx;
			font-weight: bold;
			text-align: center;
		}
		.verify {
			font-size: 28rpx;
			.meno {
				padding-bottom: 20rpx;
				color: #c8c9cc;
			}
			.v-code {
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #f4f4f5;
				padding-bottom: 10rpx;
				
				.oldcode {
					width: 60%;
					height: 70rpx;
				}
				
				.v-code-btn {
					padding: 16rpx 20rpx;
					color: #ffffff;
					background-color: #fa3534;
					border-radius: 20rpx;
				}
			}
		}
		
		.form {
			padding-top: 30rpx;
			font-size: 28rpx;
			.meno {
				font-size: 40rpx;
				padding: 30rpx 0;
				font-weight: bold;
				text-align: center;
			}
			.item {
				.phone {
					padding: 20rpx 0;
					border-bottom: 1px solid #f4f4f5;
				}
				.new-code {
					margin-top: 10rpx;
					display: flex;
					justify-content: space-between;
					border-bottom: 1px solid #f4f4f5;
					padding-bottom: 10rpx;
					
					.code {
						width: 60%;
						height: 70rpx;
					}
					
					.new-code-btn {
						padding: 16rpx 20rpx;
						color: #ffffff;
						background-color: #fa3534;
						border-radius: 20rpx;
					}
				}
			}
		}
		
		.bottom-btn {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			text-align: center;
			padding: 30rpx 0;
			background-color: #c8c9cc;
			color: #ffffff;
		}
		
		.b-on {
			background-color: #fa3534;
		}
	}
</style>
